package com.geostar.constructionland.analyse.factory;

import com.alibaba.fastjson.JSONObject;
import com.geostar.constructionland.analyse.entity.Block;
import com.geostar.constructionland.analyse.entity.WKTBlock;
import com.geostar.constructionland.analyse.entity.geohandler.GeoJsonDO;
import com.geostar.constructionland.analyse.util.WKTUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author makoto_zhu
 * @Date 2020-12-25 02:37:21
 * @Description
 **/
@Component
public class WKTBlockTypeFactory extends BlockTypeFactory {
    @Override
    public List<?> build(List<Block> blocks) {
        List<WKTBlock> wktBlocks = new ArrayList<>(blocks.size());

        blocks.forEach(block-> {
            WKTBlock wktBlock = new WKTBlock();

            String geoJson = block.getGeoJson();
            GeoJsonDO geoJsonDO = JSONObject.parseObject(geoJson, GeoJsonDO.class);

            wktBlock.setWKID(geoJsonDO.getSpatialReference().getWkid());
            wktBlock.setDK_ID(block.getDk_id());
            wktBlock.setWKT(WKTUtil.jsonToWkt("{\"type\":\"Polygon\",\"coordinates\":" + JSONObject.toJSONString(geoJsonDO.getRings()) + "}\""));

            wktBlocks.add(wktBlock);
        });


        return wktBlocks;
    }
}
